package aod.android.com.bernay.view.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Set;

import aod.android.com.bernay.BernayApp;
import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.fixing.activity.FixingAddActivity;
import aod.android.com.bernay.fixing.activity.FixingAddNameActivity;
import aod.android.com.bernay.fixing.activity.FixingFrament;
import aod.android.com.bernay.http.TempUpdataService;
import aod.android.com.bernay.live.LiveFragment;
import aod.android.com.bernay.loginandregister.BootPageActivity;
import aod.android.com.bernay.loginandregister.LoginActivity;
import aod.android.com.bernay.message.fragment.MessageFragment;
import aod.android.com.bernay.mine.fragment.MineFragment;
import aod.android.com.bernay.util.SharePreUtils;
import chen.lion.hilib.adapter.HiPagerAdapter;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.HiPagerTabLayout;
import chen.lion.hilib.view.bind.HiView;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class MainActivity extends BernayBaseActivity {

    @HiView(R.id.main_view_pager_tab)
    public HiPagerTabLayout mPagerTab;

    @HiView(R.id.main_view_pager)
    public ViewPager mViewPager;
    @HiView(R.id.lay_title)
    LinearLayout lay_title;
    public ArrayList<Fragment> mFragments = new ArrayList<>();

    public int[] mIconDefult = {R.drawable.icon_main_fixing,R.drawable.icon_main_live,R.drawable.icon_main_msg,R.drawable.icon_main_my};

    public int[] mIconSelect = {R.drawable.icon_main_sel_fixing,R.drawable.icon_main_sel_live,R.drawable.icon_main_sel_msg,R.drawable.icon_main_sel_my};

    public String[] mTabTitle = {"设备","直播","消息","我的"};
    //视频连接 暂时写死
    private static String mAccountId = "550101";
    private static String mIvccountId = "550108";
    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        if(SharePreUtils.newInstance(this).getUserId().equals("")){
            goActivity(BootPageActivity.class);
        }



        initTitle(getString(R.string.fixing_name), R.drawable.icon_fixing_add, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(MainActivity.this,FixingAddActivity.class));
            }
        });

        JPushInterface.setAlias(mContext, SharePreUtils.newInstance(MainActivity.this).getUserId(), new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                LogUtils.e("gotResult i="+i+",s="+s+",set="+new Gson().toJson(set));
            }
        });
        Intent i = new Intent(mContext,TempUpdataService.class);
        startService(i);
        mFragments.add(new FixingFrament());
        mFragments.add(new LiveFragment());
        mFragments.add(new MessageFragment());
        mFragments.add(new MineFragment());
        HiPagerAdapter adapter = new HiPagerAdapter(getSupportFragmentManager(),mFragments);
        mViewPager.setAdapter(adapter);
        mPagerTab.setColors(getResources().getColor(R.color.main_defult),getResources().getColor(R.color.main_select));
        mPagerTab.addTabs(mIconDefult,mIconSelect,mTabTitle);
        mPagerTab.setViewPager(mViewPager);
        mPagerTab.setOnPagerSelected(new HiPagerTabLayout.OnPagerSelected() {
            @Override
            public void onSelect(int position) {
                switch (position){
                    case  0 :
                        JCVideoPlayer.releaseAllVideos();
                        goneTitle(false);
                        initTitle(getString(R.string.fixing_name), R.drawable.icon_fixing_add, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(MainActivity.this,FixingAddActivity.class));
                            }
                        });
                        break;
                    case  1 :
                        goneTitle(true);
                        initTitle("直播",false);
                        break;
                    case  2 :
                        JCVideoPlayer.releaseAllVideos();
                        initTitle("消息",false);
                        goneTitle(true);
                        break;
                    case  3 :
                        goneTitle(true);
                        JCVideoPlayer.releaseAllVideos();

                        initTitle("我的",false);
                        break;
                }
            }
        });
    }
}
